package com.gs.core.aop;


import java.util.Map;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.hibernate.Session;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Aspect
@Component(value = "sessionInterestedAspect")
public class SessionInterestedAspect {
   private Logger log = Logger.getLogger(SessionInterestedAspect.class);

   @Before(value = "execution(* org.hibernate.*.openSession(..))")
   public void saveSomething(JoinPoint jp) {
        String msg = "OPEN SESSION " + jp.getSignature().getName();
        System.out.println(msg);
        log.info(msg);
        if (log.isInfoEnabled()) {
        }
        StringBuilder sb = new StringBuilder();
        sb.append("kind: " + jp.getKind());
        System.out.println(sb.toString());
        dumpTransactionSynchronizationManager("BEFORE:SHOW MAP");
   }
    @After(value = "execution(* org.hibernate.*.openSession(..))")
    public void afterOpenSession(JoinPoint jp) {
        String msg = "AFTER OPEN SESSION " + jp.getSignature().getName();
        System.out.println(msg);
        log.info(msg);
        if (log.isInfoEnabled()) {
        }
        dumpTransactionSynchronizationManager("AFTER:SHOW MAP");
    }

   private void dumpTransactionSynchronizationManager(String msg) {
       if(msg!=null){System.out.println(msg);}
       System.out.println("TransactionSynchronizationManager.isActualTransactionActive(): " + TransactionSynchronizationManager.isActualTransactionActive());
       System.out.println("TransactionSynchronizationManager.getCurrentTransactionIsolationLevel(): " + TransactionSynchronizationManager.getCurrentTransactionIsolationLevel());
       Map<Object, Object> map = TransactionSynchronizationManager.getResourceMap();
       for (Map.Entry<Object, Object> thing : map.entrySet()) {
           System.out.println(thing.getKey());
       }

   }

    @Before(value = "execution(* org.hibernate.*.close(..))")
    public void closeSession(JoinPoint jp) {
        String msg = "CLOSE SESSION REQUESTED " + jp.getSignature().getName();
        System.out.println(msg);
        if (log.isInfoEnabled()) {
            log.info(msg);
        }
    }
    @Before(value = "execution(* org.hibernate.*.disconnect(..))")
    public void disconnectSession(JoinPoint jp) {
        String msg = "disconnect CLOSE SESSION REQUESTED " + jp.getSignature().getName();
        System.out.println(msg);
        if (log.isInfoEnabled()) {
            log.info(msg);
        }
    }
    @Before(value = "execution(* org.hibernate.*.close())")
    public void morecloseSession(JoinPoint jp) {
        String msg = "MORE CLOSE SESSION REQUESTED " + jp.getSignature().getName();
        System.out.println(msg);
        if (log.isInfoEnabled()) {
            log.info(msg);
        }
    }
}



